package com.huawei.hms.framework.network.restclient.hwhttp.okhttp;

import android.text.TextUtils;
import android.util.LruCache;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import com.huawei.hms.framework.network.restclient.hwhttp.Interceptor;
import com.huawei.hms.framework.network.restclient.hwhttp.Interceptor.Chain;
import com.huawei.hms.framework.network.restclient.hwhttp.RealInterceptorChain;
import com.huawei.hms.framework.network.restclient.hwhttp.Request;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestTask;
import com.huawei.hms.framework.network.restclient.hwhttp.Response;
import com.huawei.hms.framework.network.restclient.hwhttp.model.ConnectionInfo;
import com.huawei.hms.framework.network.restclient.hwhttp.url.HttpUrl;
import java.io.IOException;
import java.net.URL;

public class MultiHostChangeInterceptor implements Interceptor {
    private static final String COLON = ":";
    private static final int DEFAULT_PORT = -1;
    private static final String EQUAL = "=";
    private static final int MAX_ENTRIES = 256;
    private static final String MULTI_HOST = "Multi-Cloud-Svc";
    private static final String SELECT_HOST = "host";
    private static final String SEMICOLON = ";";
    private static final String TAG = "MultiHostChange";
    private static final LruCache<String, ConnectionInfo> cache = new LruCache(MAX_ENTRIES);

    public Response intercept(Chain chain) throws IOException {
        boolean isHealthy;
        String host;
        Request build;
        Request request = chain.request();
        String url = request.getUrl().getUrl();
        URL url2 = new URL(url);
        String host2 = url2.getHost();
        int port = url2.getPort();
        ConnectionInfo connectionInfo = (ConnectionInfo) cache.get(host2);
        if (connectionInfo != null) {
            isHealthy = connectionInfo.isHealthy(!request.getMethod().equals(HttpContants.HTTP_METHOD_GET));
        } else {
            isHealthy = false;
        }
        Logger.v(TAG, "whether the connection is OK ? %s", Boolean.valueOf(isHealthy));
        Logger.v(TAG, "Before the MultiHostChange,the request is = %s", request);
        String str;
        if (isHealthy) {
            host = connectionInfo.getHost();
            if (host != null) {
                Object obj;
                if (port != -1) {
                    obj = host2 + COLON + port;
                } else {
                    str = host2;
                }
                build = request.newBuilder().url(new HttpUrl(url.replaceFirst(obj, host))).build();
            }
            str = host2;
            build = request;
        } else {
            cache.remove(host2);
            str = host2;
            build = request;
        }
        Logger.v(TAG, "after the MultiHost,the request is = %s", build);
        try {
            Response proceed = chain.proceed(build);
            if (proceed != null) {
                Logger.v(TAG, "the headers that you need is : %s", proceed.getHeaders().get(MULTI_HOST));
                CharSequence valueFromHeader = getValueFromHeader(host, SELECT_HOST);
                if (!TextUtils.isEmpty(valueFromHeader)) {
                    RequestTask requestTask = ((RealInterceptorChain) chain).getRequestTask();
                    if (requestTask != null) {
                        ConnectionInfo connectionInfo2 = requestTask.getConnectionInfo();
                        if (connectionInfo2 != null) {
                            cache.put(valueFromHeader, connectionInfo2);
                        } else {
                            Logger.w(TAG, "cacheMapFailed,because the message is null!");
                        }
                    }
                    Logger.v(TAG, "the cache size is %d \n %s", Integer.valueOf(cache.size()), cache.snapshot());
                }
            }
            return proceed;
        } catch (Exception e) {
            cache.remove(obj);
            throw e;
        }
    }

    private String getValueFromHeader(String str, String str2) {
        String str3 = "";
        if (str == null || str.isEmpty()) {
            return str3;
        }
        String[] split = str.split(SEMICOLON);
        for (String split2 : split) {
            String[] split3 = split2.split("=");
            if (split3.length == 2 && str2.equals(split3[0].trim())) {
                return split3[1].trim();
            }
        }
        return str3;
    }
}
